package com.ml.mall.api.cart;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ml.mall.annotations.OperLog;
import com.ml.mall.api.BaseController;
import com.ml.mall.bean.base.ApiRestResult;
import com.ml.mall.bean.req.ProductCartReq;
import com.ml.mall.bean.res.ShoppingCartQueryRes;
import com.ml.mall.consts.PermissionConst;
import com.ml.mall.entity.variant.SysProductVariantSku;
import com.ml.mall.enums.RetStatusNum;
import com.ml.mall.service.shopping.SysShoppingCartService;
import com.ml.mall.service.variant.SysProductVariantSkuService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author kyyang
 * Created 2021-02-28
 */

@RestController
@RequestMapping("/shopping")
@Api(tags = "【shopping】购物车")
@Slf4j
public class ShoppingCartController extends BaseController {

    @Autowired
    private SysShoppingCartService sysShoppingCartService;

    @Autowired
    private SysProductVariantSkuService sysProductVariantSkuService;





    @RequestMapping(method = RequestMethod.POST, path = "/new")
    @ApiOperation("添加商品到购物车")
    @OperLog(operDesc = "AddToCart", operModul = "Cart", operType = "new")
//    @RequiresPermissions(PermissionConst.SHOPPING_NEW)
    public ApiRestResult add2Cart(@RequestBody @Valid List<ProductCartReq> productCartReqs) {
        if(null != productCartReqs && productCartReqs.size()>0){
            for (ProductCartReq productCartReq : productCartReqs) {
                //校验商品数量
                if(productCartReq.getQuantity()<=0){
                    log.error("添加商品到购物车,数量为:[{}]",productCartReq.getQuantity());
                    return ApiRestResult.err("Kuantiti tidak boleh kurang daripada 0", RetStatusNum.PARAMS_ERROR.getCode());
                }
                //校验库存数
                QueryWrapper<SysProductVariantSku> wrapper = new QueryWrapper<>();
                wrapper.eq("variant_id",productCartReq.getVariantId());
                SysProductVariantSku sysProductSku = sysProductVariantSkuService.getOne(wrapper);
                if(sysProductSku==null||sysProductSku.getStock()<productCartReq.getQuantity()){
                    return ApiRestResult.err("Stok kurang! Hanya "+(sysProductSku==null?0:sysProductSku.getStock()) +" yang tinggal", RetStatusNum.PARAMS_ERROR.getCode());
                }
            }
            sysShoppingCartService.addProduct2Cart(productCartReqs);
        }

        return ApiRestResult.ok();
    }


    @RequestMapping(method = RequestMethod.POST, path = "/edit")
    @ApiOperation("修改购物车商品数量")
    @OperLog(operDesc = "EditCartProduct", operModul = "Cart", operType = "edit")
//    @RequiresPermissions(PermissionConst.SHOPPING_EDIT)
    public ApiRestResult editProduct2Cart(@RequestBody @Valid ProductCartReq productCartReq) {
        //校验商品数量
        if(productCartReq.getQuantity()<0){
            log.error("添加商品到购物车,数量为:[{}]",productCartReq.getQuantity());
            return ApiRestResult.err("Kuantiti tidak boleh kurang daripada 0", RetStatusNum.PARAMS_ERROR.getCode());
        }
        //校验库存数
        QueryWrapper<SysProductVariantSku> wrapper = new QueryWrapper<>();
        wrapper.eq("variant_id",productCartReq.getVariantId());
        SysProductVariantSku sysProductSku = sysProductVariantSkuService.getOne(wrapper);
        if(sysProductSku==null||sysProductSku.getStock() < productCartReq.getQuantity()){
            return ApiRestResult.err("Stok kurang ! Hanya"+(sysProductSku==null?0:sysProductSku.getStock()) +" yang tinggal", RetStatusNum.PARAMS_ERROR.getCode());
        }
        sysShoppingCartService.modifyProduct2Cart(productCartReq);
        return ApiRestResult.ok();
    }


    @RequestMapping(method = RequestMethod.POST, path = "/clear")
    @ApiOperation("清空购物车")
    @OperLog(operDesc = "ClearCartProduct", operModul = "Cart", operType = "Clear")
//    @RequiresPermissions(PermissionConst.SHOPPING_CLEAR)
    public ApiRestResult clearProduct2Cart() {
        sysShoppingCartService.clearCart();
        return ApiRestResult.ok();
    }


    @RequestMapping(method = RequestMethod.POST, path = "/del")
    @ApiOperation("删除购物车商品")
    @OperLog(operDesc = "DelCartProduct", operModul = "Cart", operType = "Delete")
//    @RequiresPermissions(PermissionConst.SHOPPING_DEL)
    public ApiRestResult delProduct2Cart(@RequestBody @Valid ProductCartReq productCartReq) {
        sysShoppingCartService.delProduct2Cart(productCartReq);
        return ApiRestResult.ok();
    }


    @RequestMapping(method = RequestMethod.POST, path = "/query")
    @ApiOperation("查询购物车")
//    @RequiresPermissions(PermissionConst.SHOPPING_QUERY)
    public ApiRestResult queryCurCart() {
        ShoppingCartQueryRes shoppingCartQueryRes = sysShoppingCartService.queryProduct2Cart();
        return ApiRestResult.ok(shoppingCartQueryRes);
    }


}